import { createRouter, createWebHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'
import { useAuthStore } from '@/stores/auth'

const routes: RouteRecordRaw[] = [
  {
    path: '/',
    name: 'Home',
    component: () => import('../views/Home.vue')
  },
  {
    path: '/tutorials',
    name: 'Tutorials',
    component: () => import('../views/Tutorials.vue')
  },
  {
    path: '/videos',
    name: 'Videos',
    component: () => import('../views/Videos.vue')
  },
  {
    path: '/cases',
    name: 'Cases',
    component: () => import('../views/Cases.vue')
  },
  {
    path: '/community',
    name: 'Community',
    component: () => import('../views/Community.vue')
  },
  {
    path: '/resources',
    name: 'Resources',
    component: () => import('../views/Resources.vue')
  },
  // 文档中心路由
  {
    path: '/docs',
    component: () => import('../layouts/DocLayout.vue'),
    children: [
      {
        path: '',
        name: 'docs-home',
        component: () => import('../views/docs/DocList.vue')
      },
      {
        path: 'category/:category',
        name: 'docs-category',
        component: () => import('../views/docs/DocList.vue')
      },
      {
        path: 'lesson/:id',
        name: 'docs-lesson',
        component: () => import('../views/docs/DocContent.vue')
      },
      {
        path: 'search',
        name: 'docs-search',
        component: () => import('../views/docs/DocList.vue')
      }
    ]
  },
  // 后台管理路由
  {
    path: '/admin/login',
    name: 'AdminLogin',
    component: () => import('../views/admin/Login.vue'),
    meta: { requiresGuest: true }
  },
  {
    path: '/admin',
    redirect: '/admin/dashboard',
    component: () => import('../layouts/AdminLayout.vue'),
    meta: { requiresAuth: true },
    children: [
      {
        path: 'dashboard',
        name: 'AdminDashboard',
        component: () => import('../views/admin/Dashboard.vue'),
        meta: { title: '仪表盘' }
      },
      {
        path: 'categories',
        name: 'AdminCategories',
        component: () => import('../views/admin/Categories.vue'),
        meta: { title: '分类管理' }
      },
      {
        path: 'lessons',
        name: 'AdminLessons',
        component: () => import('../views/admin/Lessons.vue'),
        meta: { title: '课程管理' }
      },
      {
        path: 'lessons/create',
        name: 'AdminLessonCreate',
        component: () => import('../views/admin/LessonForm.vue'),
        meta: { title: '创建课程' }
      },
      {
        path: 'lessons/:id/edit',
        name: 'AdminLessonEdit',
        component: () => import('../views/admin/LessonForm.vue'),
        meta: { title: '编辑课程' }
      },
      {
        path: 'tags',
        name: 'AdminTags',
        component: () => import('../views/admin/Tags.vue'),
        meta: { title: '标签管理' }
      },
      {
        path: 'code-examples',
        name: 'AdminCodeExamples',
        component: () => import('../views/admin/CodeExamples.vue'),
        meta: { title: '代码示例管理' }
      },
      {
        path: 'files',
        name: 'AdminFiles',
        component: () => import('../views/admin/Files.vue'),
        meta: { title: '文件管理' }
      },
      {
        path: 'profile',
        name: 'AdminProfile',
        component: () => import('../views/admin/Profile.vue'),
        meta: { title: '个人资料' }
      },
      // 文档管理
      {
        path: 'docs/categories',
        name: 'AdminDocCategories',
        component: () => import('../views/admin/docs/DocCategories.vue'),
        meta: { title: '文档分类管理' }
      },
      {
        path: 'docs/list',
        name: 'AdminDocList',
        component: () => import('../views/admin/docs/DocList.vue'),
        meta: { title: '文档列表管理' }
      },
      {
        path: 'docs/import',
        name: 'AdminDocImport',
        component: () => import('../views/admin/docs/DocImport.vue'),
        meta: { title: '文档导入' }
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const authStore = useAuthStore()
  
  // 初始化认证状态
  if (!authStore.isAuthenticated) {
    authStore.initAuth()
  }

  if (to.meta.requiresAuth && !authStore.isAuthenticated) {
    // 需要认证但未登录，重定向到登录页
    next('/admin/login')
  } else if (to.meta.requiresGuest && authStore.isAuthenticated) {
    // 需要游客状态但已登录，重定向到后台首页
    next('/admin/dashboard')
  } else {
    next()
  }
})

export default router 